#include "glfw_monitor.h"
#include <GLFW/glfw3.h>

namespace veng {

gsl::span<GLFWmonitor*> GetMonitors() {
  // 这一段代码是获得多个显示的，然后将窗口显示到第二个显示器上面。
  std::int32_t monitor_count;
  auto monitor_pointers = glfwGetMonitors(&monitor_count);
  return gsl::span<GLFWmonitor*>(monitor_pointers, monitor_count);
}

glm::ivec2 GetMonitorPosition(gsl::not_null<GLFWmonitor*> monitor) {
  glm::ivec2 monitor_pos, monitor_size;
  glfwGetMonitorPos(monitor, &monitor_pos.x, &monitor_pos.y);
  return monitor_pos;
}

glm::ivec2 GetMonitorWorkAreaSize(gsl::not_null<GLFWmonitor*> monitor) {
  glm::ivec2 monitor_size;
  glfwGetMonitorWorkarea(monitor, nullptr, nullptr, &monitor_size.x, &monitor_size.y);
  return monitor_size;
}

glm::ivec2 MoveWindowToMonitor(gsl::not_null<GLFWwindow*> window, gsl::not_null<GLFWmonitor*> monitor) {
  // 使窗口置中
  glm::ivec2 monitor_pos = GetMonitorPosition(monitor);
  glm::ivec2 monitor_size = GetMonitorWorkAreaSize(monitor);
  glm::ivec2 window_size;
  glfwGetWindowSize(window, &window_size.x, &window_size.y);
  const glm::ivec2 window_new_position = monitor_pos + (monitor_size / 2) - (window_size / 2);
  glfwSetWindowPos(window, window_new_position.x, window_new_position.y);
  return window_new_position;
}

}  // namespace veng
